package com.xyb.service;

import com.xyb.config.exception.MyException;
import com.xyb.dao.SysUserDao;
import com.xyb.dao.SysUserRoleDao;
import com.xyb.entity.StatusCode;
import com.xyb.entity.SysUser;
import com.xyb.entity.SysUserRole;
import com.xyb.utils.IdUtils;
import com.xyb.vo.SysUserVo;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysUserRoleDao sysUserRoleDao;
    @Resource
    private IdUtils idUtils;
    @Resource
    private PasswordEncoder passwordEncoder;

    public List<SysUser> selectSysUserListByStatus(Integer status) {
        List<SysUser> userList = sysUserDao.selectSysUserListByStatus(status);
        userList.forEach(i -> {
            i.setUserPwd("******");
        });
        return userList;
    }

    public SysUserVo selectSysUserByUserId(Long userId) {
        SysUser sysUser = sysUserDao.selectSysUserByUserId(userId);
        List<SysUserRole> userRoleList = sysUserRoleDao.selectSysUserByUserId(userId);
        List<Long> list = new ArrayList<>();
        userRoleList.forEach(i -> {
            list.add(i.getRoleId());
        });
        SysUserVo sysUserVo = SysUserVo.builder()
                .userId(sysUser.getUserId())
                .userName(sysUser.getUserName())
                .userNickName(sysUser.getUserNickName())
                .iphoneNo(sysUser.getIphoneNo())
                .userPwd("******")
                .headPortraitUrl(sysUser.getHeadPortraitUrl())
                .status(sysUser.getStatus())
                .showOrder(sysUser.getShowOrder())
                .createTime(sysUser.getCreateTime())
                .roleIds(list)
                .build();
        return sysUserVo;
    }

    public String insertSysUser(SysUserVo vo) {
        SysUser user = sysUserDao.selectSysUserByIphoneNo(vo.getIphoneNo());
        if (user != null) {
            throw new MyException(StatusCode.PHONE_HAS_REGISTERED_EXCEPTION); //手机号已经注册
        }
        SysUser sysUser = SysUser.builder()
                .userId(idUtils.nextId())
                .userName(vo.getUserName())
                .userNickName(vo.getUserNickName())
                .iphoneNo(vo.getIphoneNo())
                .userPwd(vo.getUserPwd() == null ? null : passwordEncoder.encode("123456"))
                .headPortraitUrl("https://img0.baidu.com/it/u=2809737200,31955359&fm=26&fmt=auto&gp=0.jpg")
                .status(1)
                .createTime(new Date())
                .build();
        sysUserDao.insertSysUser(sysUser);
        return "success";
    }

    public String updateSysUserStatus(SysUserVo vo) {
        if (vo.getUserId().intValue() == 0 && vo.getStatus().intValue() == 0) {
            throw new MyException(StatusCode.ADMIN_REFUSED_EXCEPTION);
        }
        SysUser sysUser = SysUser.builder()
                .userId(vo.getUserId())
                .status(vo.getStatus())
                .build();
        sysUserDao.updateSysUserByUserId(sysUser);
        return "success";
    }

    public String updateSysUser(SysUserVo vo) {
        String pwd = vo.getUserPwd();

        //更新用户信息
        SysUser sysUser = SysUser.builder()
                .userId(vo.getUserId())
                .userName(vo.getUserName())
                .userNickName(vo.getUserNickName())
                .iphoneNo(vo.getIphoneNo())
                .userPwd((pwd == null || "".equals(pwd.trim())) ? null : passwordEncoder.encode(pwd))
                .headPortraitUrl(vo.getHeadPortraitUrl())
                .status(vo.getStatus())
                .build();
        sysUserDao.updateSysUserByUserId(sysUser);
        //管理员不可修改
        if (vo.getUserId().intValue() == 0) {
            return "success";
        }
        //删除用户角色
        sysUserRoleDao.deleteSysUserByUserId(vo.getUserId());
        //添加用户角色
        List<Long> roleIds = vo.getRoleIds();
        if (roleIds != null && roleIds.size() > 0) {
            List<SysUserRole> list = new ArrayList<>();
            roleIds.forEach(i -> {
                SysUserRole sysUserRole = SysUserRole.builder()
                        .userRoleId(idUtils.nextId())
                        .userId(vo.getUserId())
                        .roleId(i)
                        .build();
                list.add(sysUserRole);
            });
            sysUserRoleDao.insertSysUserRoleBySysUserRoleList(list);
        }
        return "success";
    }

    public String deleteSysUserByUserId(Long userId) {
        if (userId.intValue() == 0) {
            throw new MyException(StatusCode.ADMIN_REFUSED_EXCEPTION);
        }
        sysUserDao.deleteSysUserByUserId(userId);
        sysUserRoleDao.deleteSysUserByUserId(userId);
        return "success";
    }

    public String updateSysUserShowOrder(Long[] userIds) {
        List<SysUser> list = new ArrayList<>();
        for (int i = 0; i < userIds.length; i++) {
            SysUser sysUser = new SysUser();
            Long userId = userIds[i];
            sysUser.setUserId(userId);
            sysUser.setShowOrder((long)i);
            list.add(sysUser);
        }
        sysUserDao.updateSysUserBySysUserList(list);
        return "success";
    }
}


















































